package com.yxhl.zoume.core.expresscar.helper;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.yxhl.zoume.utils.DeviceUtils;

import javax.inject.Inject;

/**
 * Author：zhongshan   
 * Date：2016/12/12 16:33      
 * Description：司机信息，订单状态，时间行程 的  动画调度
 */
public class ScheduleAnimatorHelper {

    public static final int PICK_UP_MOVE_DURATION = 600;
    public static final int DRIVER_INFO_MOVE_DURATION = 400;
    private AnimationDrawable mAnimationDrawable;

    @Inject
    public ScheduleAnimatorHelper() {
    }

    /**
     * 司机信息栏进入动画
     *
     * @param driverInfoRl             司机信息布局
     * @param headerContainerRl        司机信息和订单处理状态信息顶层布局
     * @param isRunPickUpEnterAnimator 是否需要显示
     */
    public void startEnterAnimatorForDriverInfo(RelativeLayout driverInfoRl,
                                                RelativeLayout headerContainerRl,
                                                final boolean isRunPickUpEnterAnimator) {
        int height = driverInfoRl.getHeight();
        ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(headerContainerRl, "translationY", 0, height);
        translateAnimator.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(driverInfoRl, "alpha", 0, 1f);

        AnimatorSet enterAnimatorSet = new AnimatorSet();
        enterAnimatorSet.setDuration(DRIVER_INFO_MOVE_DURATION);
        enterAnimatorSet.playTogether(translateAnimator, alphaAnimator);
        enterAnimatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });

        enterAnimatorSet.start();
    }

    /**
     * 司机信息栏退出动画
     */
    public void startExitAnimatorForDriverInfo(RelativeLayout mDriverInfoRl, RelativeLayout mHeaderContainerRl) {
        int height = mDriverInfoRl.getHeight();
        ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(mHeaderContainerRl, "translationY", height, 0);
        translateAnimator.setInterpolator(new AccelerateInterpolator());
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mDriverInfoRl, "alpha", 1f, 0.6f);

        AnimatorSet exitAnimatorSet = new AnimatorSet();
        exitAnimatorSet.setDuration(DRIVER_INFO_MOVE_DURATION);
        exitAnimatorSet.playTogether(translateAnimator, alphaAnimator);

        exitAnimatorSet.start();
    }

    /**
     * 订单信息栏进入动画 & 小车gif动画
     */
    public void startEnterAnimatorForScheduleBar(ImageView scheduleIconIv, RelativeLayout scheduleBarRl) {
        //开启小车帧动画
        mAnimationDrawable = (AnimationDrawable) scheduleIconIv.getDrawable();
        mAnimationDrawable.start();

        int deltaTranslationX = getDeltaTranslationX(scheduleBarRl);
        ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(scheduleBarRl, "translationX", 0, deltaTranslationX);
        translateAnimator.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(scheduleBarRl, "alpha", 0.3f, 1);
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(scheduleBarRl, "scaleX", 0.3f, 1);

        AnimatorSet enterAnimatorSet = new AnimatorSet();
        enterAnimatorSet.setDuration(PICK_UP_MOVE_DURATION);
        enterAnimatorSet.playTogether(translateAnimator, alphaAnimator, scaleXAnimator);

        enterAnimatorSet.start();
    }

    /**
     * 获取 动画移动所需的距离
     *
     * @return
     */
    private int getDeltaTranslationX(View view) {
        int screenWidth = DeviceUtils.getScreenWidth(view.getContext());
        int pickUpRlWidth = view.getWidth();
        int ret = (screenWidth - pickUpRlWidth) / 2 + pickUpRlWidth;
        return ret;
    }

    /**
     * 订单信息栏退出动画 && 小车gif动画关闭
     *
     * @param
     */
    public void startExitAnimatorForScheduleBar(RelativeLayout scheduleBarRl) {
        //关闭小车帧动画
        if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
            mAnimationDrawable.stop();
        }
        int deltaTranslationX = getDeltaTranslationX(scheduleBarRl);
        ObjectAnimator translateAnimator = ObjectAnimator.ofFloat(scheduleBarRl, "translationX", deltaTranslationX, 0);
        translateAnimator.setInterpolator(new AccelerateInterpolator());
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(scheduleBarRl, "alpha", 1, 0.3f);
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(scheduleBarRl, "scaleX", 1, 0.3f);

        AnimatorSet exitAnimatorSet = new AnimatorSet();
        exitAnimatorSet.setDuration(PICK_UP_MOVE_DURATION);
        exitAnimatorSet.playTogether(translateAnimator, alphaAnimator, scaleXAnimator);
        exitAnimatorSet.start();
    }

    /**
     * 启动/退出底栏弹出框
     *
     * @param needUpTranslation true,需要向上弹出,false,需要向下退出弹框
     */
    public void startTimeDistanceInfoBarAnimator(LinearLayout mScheduleTimeDistanceBar, boolean needUpTranslation) {

        int height = mScheduleTimeDistanceBar.getHeight();

        PropertyValuesHolder translationValuesHolder;
        PropertyValuesHolder alphaValuesHolder;
        ObjectAnimator translationAnimator;
        //上移
        if (needUpTranslation) {
            translationValuesHolder = PropertyValuesHolder.ofFloat("translationY", 0, (-1) * height);
            alphaValuesHolder = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
            translationAnimator = ObjectAnimator.ofPropertyValuesHolder(
                    mScheduleTimeDistanceBar,
                    translationValuesHolder,
                    alphaValuesHolder
            );
            translationAnimator.setInterpolator(new DecelerateInterpolator());
        }
        //下移
        else {
            translationValuesHolder = PropertyValuesHolder.ofFloat("translationY", (-1) * height, 0);
            alphaValuesHolder = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
            translationAnimator =
                    ObjectAnimator.ofPropertyValuesHolder(
                            mScheduleTimeDistanceBar,
                            translationValuesHolder,
                            alphaValuesHolder
                    );
            translationAnimator.setInterpolator(new AccelerateInterpolator());
        }

        translationAnimator.setDuration(300);
        translationAnimator.start();
    }

}
